package com.chookss.home.adapter;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.Spanned;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.WebCommonActivity;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.base.StaticClass;
import com.chookss.base.Token;
import com.chookss.home.audit.clockIn.ClockInSureActivity;
import com.chookss.home.entity.NoticeListEntity;
import com.chookss.tools.Utils;
import com.chookss.view.MyImageSpan;
import com.johnrambo.ktea.ktExtends.ShareUtils;

import java.util.List;

import io.reactivex.annotations.Nullable;

public class NoticeListsAdapter extends BaseQuickAdapter<NoticeListEntity, BaseViewHolderEx> {
    private Context context;

    public NoticeListsAdapter(int layoutResId, Context context, @Nullable List<NoticeListEntity> data) {
        super(layoutResId, data);
        this.context = context;
    }

    @Override
    protected void convert(BaseViewHolderEx helper, NoticeListEntity item) {
        TextView tvTitle = helper.getView(R.id.tvNoticeTitle);
        TextView tvContent = helper.getView(R.id.tvNoticeContent);
        TextView tvDate = helper.getView(R.id.tvNoticeTime);

        String title = item.getNoticeTitle().trim().replace("\r|\n", "");
        if (Utils.isNull(item.getEmployeeCode())) {
            SpannableString spannableString = new SpannableString("  " + title);
            Drawable drawable = context.getResources().getDrawable(R.drawable.weidu_gg);
            drawable.setBounds(
                    0,
                    0,
                    Utils.dip2px(context, 23f),
                    Utils.dip2px(context, 12f)
            );
            MyImageSpan imageSpan = new MyImageSpan(drawable);
            spannableString.setSpan(imageSpan, 0, 1, Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
            tvTitle.setText(spannableString);
        } else {
            tvTitle.setText(title);
        }
        tvContent.setText(item.getNoticeContent());
        tvDate.setText(Utils.getAccurateDate(item.getCreateTime()));
        helper.getItemView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int confim=Utils.changeNumber(item.getConfirm());
                if (confim==0) {
                    String addressType = "";
                    if (Urls.baseUrl.contains("dev_kmer")) {
                        addressType = "1";
                    } else {
                        addressType = "0";
                    }
                    String url = Urls.webBaseUrl + "indexNotice.html#/noticeDetail?noticeCode=" + item.getNoticeCode() + "&token=" + Token.INSTANCE.getToken() + "&statusHeight=" + Utils.getStatusBarHeightDp(context) + "&time=" + System.currentTimeMillis() + "&addressType=" + addressType;
                    Intent intent = new Intent(context, WebCommonActivity.class);
                    intent.putExtra("url", url);
                    intent.putExtra("waterMark", "12");
                    context.startActivity(intent);
                } else {
                    Intent intent = new Intent(context, ClockInSureActivity.class);
                    intent.putExtra("time", item.getCreateTime());
                    intent.putExtra("noticeCode", item.getNoticeCode());
                    context.startActivity(intent);
                    if(Utils.isNull(item.getNoticeCode())){
                        item.setEmployeeCode(new ShareUtils().getString(context, StaticClass.EMPLOYEECODE,""));
                        notifyItemInserted(helper.getItemPosition());
                    }
                }
            }
        });

    }

}
